
package com.ukao.cashregister.pesenter;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.CameraTokenBean;
import com.ukao.cashregister.bean.ProductRelImgBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.CameraView;

import java.util.HashMap;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class CameraPesenter extends BasePresenter<CameraView> {
    private boolean isloaderr;

    public CameraPesenter(CameraView view) {
        attachView(view);
    }

    /**
     *获取七牛token
     */
    public void qiniutoken( ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.qiniutoken(Constant.createParameter(mHashMap)),
                new ApiCallback<CameraTokenBean>() {
                    @Override
                    public void onSuccess(CameraTokenBean model) {
                        mvpView.hideLoading();
                        if (model.getHttpCode()==200){
                            mvpView.loadQiLitoken(model.getData().getToken());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();

                    }

                });
    }


    /**
      添加衣物照片
     orderProId 订单衣物id
     cameraImg 拍照图片路径
     compressImg 拍照压缩后的图片路径
     */
    public void addphotoOrder(final   String   orderProId,String  compressImg  ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("imgs",compressImg);
       // mHashMap.put("compressImg",compressImg);
        mHashMap.put("orderProId", orderProId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.addroductRelImg(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode()==200){
                            mvpView.addphotoSucceed();
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();

                    }

                });
    }



    /**
     删除衣物照片
     */
    public void deleteRelImg(final ProductRelImgBean item    ) {
        mvpView.showLoading();
         String  cameraImg=item.getCameraImg();
        if (!CheckUtils.isEmpty(cameraImg) && !cameraImg.substring(0, 4).equals("http")) {
            mvpView.deletePhoto(item);
            mvpView.hideLoading();
        }else{
            HashMap mHashMap=new HashMap<String,String>();
            mHashMap.put("id", String.valueOf(item.getId()));/*衣物图片的id*/
            mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
            addSubscription(apiStores.deleteRelImg(Constant.createParameter(mHashMap)),
                    new ApiCallback<StringBean>() {
                        @Override
                        public void onSuccess(StringBean model) {
                            mvpView.hideLoading();
                            if (model.getHttpCode()==200){
                                mvpView.deletePhoto(item);
                            }else{
                                mvpView.loadFail(model.getMsg());
                            }

                        }



                        @Override
                        public void onFinish() {
                            mvpView.hideLoading();

                        }

                    });
        }

    }






}
